package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.Manifest;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.NearByShopBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.NearByShopPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter.NearbyShopAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.NearByShopView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/23.
 * Desc   :
 */

public class NearbyShopActivity extends BaseActivity<NearByShopPresenter> implements NearByShopView, AMapLocationListener {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_auto_sort)
    TextView tvAutoSort;
    @BindView(R.id.tv_score_sort)
    TextView tvScoreSort;
    @BindView(R.id.tv_near_sort)
    TextView tvNearSort;

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    private PageRecorder pageRecorder = new PageRecorder();
    //智能排序：1；评分排序：2；附近推荐：3
    private String type = "1";
    private double latitude;
    private double longitude;
    private NearbyShopAdapter adapter;
    private String city;
    private String searchShopName;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_nearby_shop);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new NearByShopPresenter(this);
    }

    @Override
    protected void init() {
        tvAutoSort.setSelected(true);
        //6.0以上权限获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) permissionCheck();
        List<NearByShopBean> list = new ArrayList<>();
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new NearbyShopAdapter(mContext, R.layout.item_nearby_shop, list);
        recyclerview.setAdapter(adapter);
        adapter.setmOnItemClickListener((arg0, arg1) -> startActivity(NearbyShopDetailActivity.createIntent(this, adapter.getDatas().get(arg1).getId())));
    }

    private void permissionCheck() {
        PermissionGen.with(this)
                .addRequestCode(100)
                .permissions(
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .request();
    }

    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        city = aMapLocation.getCity();
        latitude = aMapLocation.getLatitude();
        longitude = aMapLocation.getLongitude();
        getShopList();
    }

    @PermissionFail(requestCode = 100)
    public void doFailSomething() {
        finish();
    }

    @PermissionSuccess(requestCode = 100)
    public void success() {
        initLocation();
    }

    @OnClick({R.id.ll_topleft, R.id.tv_auto_sort, R.id.tv_score_sort, R.id.tv_near_sort})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_topleft:
                finish();
                break;
            case R.id.tv_auto_sort:
                type = "1";
                tvAutoSort.setSelected(true);
                tvScoreSort.setSelected(false);
                tvNearSort.setSelected(false);
                getShopList();
                break;
            case R.id.tv_score_sort:
                type = "2";
                tvAutoSort.setSelected(false);
                tvScoreSort.setSelected(true);
                tvNearSort.setSelected(false);
                getShopList();
                break;
            case R.id.tv_near_sort:
                type = "3";
                tvAutoSort.setSelected(false);
                tvScoreSort.setSelected(false);
                tvNearSort.setSelected(true);
                getShopList();
                break;
        }
    }

    private void getShopList() {
        if (latitude == 0) {
            ToastUtils.showToast("定位中");
        } else {
            mPresenter.getShopList(type, String.valueOf(latitude), String.valueOf(longitude), String.valueOf(pageRecorder.getFirstPage()), searchShopName, city);
        }
    }

    @Override
    public void setNearby(List<NearByShopBean> data) {
        adapter.setDatas(data);
    }
}
